Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[자동차 경주] 이해승 미션 제출합니다. #1467

Open
wants to merge 27 commits into
base: main
Choose a base branch
from

Conversation

lee-haeseung
Copy link

java-racingcar-precourse

기능 목록

  • 주어진 횟수 동안 n대의 자동차는 전진 또는 멈출 수 있다.
  • 각 자동차에 이름을 부여할 수 있다. 전진하는 자동차를 출력할 때 자동차 이름을 같이 출력한다.
  • 자동차 이름은 쉼표(,)를 기준으로 구분하며 이름은 5자 이하만 가능하다.
  • 사용자는 몇 번의 이동을 할 것인지를 입력할 수 있어야 한다.
  • 전진하는 조건은 0에서 9 사이에서 무작위 값을 구한 후 무작위 값이 4 이상일 경우이다.
  • 자동차 경주 게임을 완료한 후 누가 우승했는지를 알려준다. 우승자는 한 명 이상일 수 있다.
  • 우승자가 여러 명일 경우 쉼표(,)를 이용하여 구분한다.
  • 사용자가 잘못된 값을 입력할 경우 IllegalArgumentException을 발생시킨 후 애플리케이션은 종료되어야 한다.

구현할 기능

자동차

  • 주어진 횟수 동안 자동차의 전진/정지 기능
  • 자동차에 이름 부여 기능
  • 0에서 9까지의 무작위 값 구하기 및 4 이상인 경우 전진
  • 예외 처리
    • 이름 없는 경우
    • 자동차 이름이 5글자를 초과한 경우

입력

  • 쉼표(,)를 기준으로 구분된 5자 이하의 자동차 이름 입력
  • 이동 횟수 입력
  • 잘못된 값을 입력한 경우 예외 처리
    • 같은 이름의 자동차가 들어오는 경우
    • 자동차 이름이 5글자를 초과한 경우
    • 이름이 없는 경우 → 오류 출력
    • 시도하는 횟수 입력이 올바르지 않은 경우

출력

  • 자동차 출력 (이름과 전진한 정도)
  • 우승자 출력
    • 여러명인 경우 쉼표(,)로 구분하여 출력

구현할 기능 목록 정리
Model - car
View - output
Controller - RacingCarController
위와 같이 구성
자동차 이름이 없는 경우,
오류를 출력하는 것이 더 일반적으로 생각되어 기능 수정
자동차의 초기 위치가 정상적으로 결정되는지,
자동차 이름이 정상적으로 들어왔는지,
자동차가 잘 움직이는지,
자동차가 확률적으로 움직여지는지
등에 대한 car test코드 작성
이름 설정 후 확인하는 테스트 누락되어 추가
자동차 생성자 및 기본 구현 수행
이름 부여 가능하도록 구현
확률적으로 움직이는 기능이 구현됨에 따라
단순 전진/정지 기능은 private로 대체.

이에 따른 test 코드 또한 제거
람다식으로 수적
view와 controller에 대한 부분은 함께 test 진행
model 구현시 구현한 예외 처리로 인해 해결된 예외 사항 기록
Copy link

@StoneCAU StoneCAU left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2주 차 고생많으셨습니다 😄
예외처리를 꼼꼼히 처리하신 부분이 인상깊네요!
3주 차도 파이팅 해봐요~!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Application과 Controller의 역할이 중복되는 것 같은데 통합된다면 좋을 것 같습니다!

Comment on lines +11 to +14
public RacingCarController(Output output, List<Car> cars) {
this.output = output;
this.cars = cars;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

매개변수로 받아 컨트롤러를 생성하기보다는

Suggested change
public RacingCarController(Output output, List<Car> cars) {
this.output = output;
this.cars = cars;
}
public RacingCarController(Output output, List<Car> cars) {
this.output = new Output;
this.cars = new Cars;
}

이런식으로 생성하는 생성하는게 나을 것 같아요

public class Application {
public static void main(String[] args) {
// TODO: 프로그램 구현
System.out.println("경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)");
String carNameStrings = Console.readLine();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View에서 Output과 Input으로도 나누어 볼 수 있겠네요!

Comment on lines +16 to +42
public void play(int time) {
output.printResultString();
for (int i = 0; i < time; i++) {
for (Car car : cars) {
car.randomMove();
}
output.printCars(cars);
}
List<String> winners = findWinner();
output.printWinners(winners);
}

public List<String> findWinner() {
List<String> winners = new ArrayList<>();
int maxLocation = 0;

for (Car car : cars) {
if (maxLocation < car.getLocation()) {
winners.clear();
winners.add(car.getName());
} else if (maxLocation == car.getLocation()) {
winners.add(car.getName());
}
}

return winners;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 부분(비지니스 로직)은 model 쪽에서 관리할 수 있게 하는게 좋을 것 같아요.
controller의 역할은 입/출력 또는 Model과 View의 매개체라고 생각하시면 될 것같습니다!

Comment on lines +9 to +16
public Car(String name) {
location = 0;
name = name.trim();
if (name.isEmpty() || name.length() > 5) {
throw new IllegalArgumentException();
}
this.name = name;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

검증하는 부분을 함수로 처리한다면 가독성이 높아질 것 같습니다.

Suggested change
public Car(String name) {
location = 0;
name = name.trim();
if (name.isEmpty() || name.length() > 5) {
throw new IllegalArgumentException();
}
this.name = name;
}
public Car(String name) {
location = 0;
name = name.trim();
validateName(name);
this.name = name;
}
validateName이라는 메서드를 만들어서 이런식으로 처리하면 좋습니다!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Output에 대한 분리는 잘하신 것 같습니다!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자동차 이름 예외처리를 꼼꼼하게 처리하셨네요!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants